﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Data;
using Torch.FrameWork.Utility;

namespace Torch.WL.Web.CleanChina
{
    public partial class testexcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

     

        protected void Button1_Click(object sender, EventArgs e)
        {

            ExcelHelper excel = new ExcelHelper();

            ExcelSheet excelsheet = null;


            excelsheet = excel.AddSheet("数据1");
            DataTable dt1 = CreateDataSource("table1");
            excelsheet.AddDataSource(dt1, true);
            excelsheet.SetListColStyle(dt1);
            excelsheet.AutoWidth = true;


            excelsheet = excel.AddSheet("数据2");
            DataTable dt2 = CreateDataSource("table2");
            excelsheet.AddDataSource(dt2, true);
            excelsheet.SetListColStyle(dt2);
            excelsheet.AutoWidth = true;

            excelsheet = excel.AddSheet("数据3");
            DataTable dt3 = CreateDataSource("table3");
            excelsheet.AddDataSource(dt3, true);
            excelsheet.SetListColStyle(dt3);
            excelsheet.AutoWidth = true;

            excel.DataBind();

            excel.Save(HttpUtility.UrlEncode("文件名") + ".xls", this.Page.Response);

        }



        DataTable CreateDataSource(string table)
        {
            DataTable dt = new DataTable(table);

            dt.Columns.Add("id", typeof(Int32));
            dt.Columns.Add("name", typeof(String));
            dt.Columns.Add("submitdate", typeof(DateTime));
            dt.Columns.Add("amount", typeof(decimal));

            DataRow dr = null;
            for (int i = 1; i <= 10000; i++)
            {
                dr = dt.NewRow();
                dr[0] = i;
                dr[1] = "name"+i.ToString();
                dr[2] = DateTime.Now.AddDays(new Random().Next(0,1000));
                dr[3] = new Random().NextDouble()*10000.0;

                dt.Rows.Add(dr);
            }

            return dt;

        }







    }
}